package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.POJO.Member;
import com.itheima.POJO.Order;
import com.itheima.POJO.OrderSetting;
import com.itheima.POJO.Setmeal;
import com.itheima.VO.MapVO;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public interface orderMapper {

    //分页查询预约列表
    @Select("<script>" +
            "SELECT o.id,o.orderDate,o.orderType,o.orderStatus,m.name,m.phoneNumber telephone,a.address \n" +
            "FROM (t_order o LEFT JOIN t_member m ON o.member_id = m.id) LEFT JOIN t_address a \n" +
            "ON a.id=o.address_id \n" +
            "<where>" +
            "        <if test=\"value != null and value.length >0\">\n" +
            "             (m.name like \"%\"#{value}\"%\" or m.phoneNumber like \"%\"#{value}\"%\")\n" +
            "        </if>" +
            "</where>" +
            "</script>")
    Page<Map> pageQuery(String queryString);

    //根据id删除预约信息
    @Delete("DELETE FROM t_order WHERE id = #{id}")
    void deleteOrderById(Integer id);

    //根据条件查询
    @Select("<script>" +
            "select * from t_order\n" +
            "        <where>\n" +
            "            <if test=\"id != null\">\n" +
            "                and id = #{id}\n" +
            "            </if>\n" +
            "            <if test=\"memberId != null\">\n" +
            "                and member_id = #{memberId}\n" +
            "            </if>\n" +
            "            <if test=\"orderDate != null\">\n" +
            "                and orderDate = #{orderDate}\n" +
            "            </if>\n" +
            "            <if test=\"orderType != null\">\n" +
            "                and orderType = #{orderType}\n" +
            "            </if>\n" +
            "            <if test=\"orderStatus != null\">\n" +
            "                and orderStatus = #{orderStatus}\n" +
            "            </if>\n" +
            "            <if test=\"setmealId != null\">\n" +
            "                and setmeal_id = #{setmealId}\n" +
            "            </if>\n" +
            "        </where>" +
            "</script>")
    public List<Order> findByCondition(Order order);


    @Select("select * from t_ordersetting where orderDate = #{orderDate}")
    OrderSetting findOrdersettingByorderDate(String orderDate);

    @Select("select * from t_member where name = #{name}")
    Member findMemberByName(String name);


    @Select("SELECT * FROM t_order WHERE member_id = #{memberId}")
    List<Order> findOederBymemberID(Integer memberId);

    @Select("select * from t_order where member_id = #{memberId} and orderDate = #{orderDate} and setmeal_id = #{setmealId}")
    Order findBymemberIDANDorderDate(@Param("memberId") Integer memberId,@Param("orderDate") String orderDate,@Param("setmealId") Integer setmealId);


    @Insert("insert into t_order (member_id,orderDate,orderType,orderStatus,setmeal_id) values (#{memberId},#{orderDate},#{orderType},#{orderStatus},#{setmealId})")
    @SelectKey(keyProperty = "id",statement = "select last_insert_id();",resultType = int.class  ,before = false)
    void addOrder(Order order);

    @Insert("insert into t_member (fileNumber,name,sex,idCard,phoneNumber,regTime,password,email,birthday,remark,age,marriage) values (#{fileNumber},#{name},#{sex},#{idCard},#{phoneNumber},#{regTime},#{password},#{email},#{birthday},#{remark},#{age},#{marriage})")
    @SelectKey(keyProperty = "id",statement = "select last_insert_id();",resultType = int.class  ,before = false)
    void addMember(Member member);


    @Update("update t_ordersetting set reservations = #{reservations} where orderDate = #{orderDate}")
    void editReservations(OrderSetting orderSetting);

    /*
     private Integer id;
    private Integer memberId;//会员id
    private Date orderDate;//预约日期
    private String orderType;//预约类型 电话预约/微信预约
    private String orderStatus;//预约状态（是否到诊）
    private Integer setmealId;//体检套餐id
     */
    @Select("select * from t_order where id = #{id} ")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "memberId",column = "member_id"),
            @Result(property = "orderDate",column = "orderDate"),
            @Result(property = "orderType",column = "orderType"),
            @Result(property = "orderStatus",column = "orderStatus"),
            @Result(property = "setmealId",column = "setmeal_id"),
    })
    Order findorderByID(Integer id);

    @Select("select name from t_setmeal where id = #{setmealId}")
    String findSetmealByID(Integer setmealId);

    @Select("SELECT NAME FROM t_member WHERE id =#{id}")
    String findMemberBYid(Integer id);

    @Select(" SELECT COUNT(*) FROM  t_order WHERE orderStatus = '已到诊' AND orderDate = #{dateString}")
    Integer findOrderStatusCountByDate(String dateString);

    @Select("SELECT COUNT(*) FROM t_order WHERE orderStatus = '已到诊' AND orderDate BETWEEN #{startDay} AND #{endDay}")
    Integer findStatusByWeek(HashMap<String, String> hashMap);

    @Select("SELECT COUNT(*) FROM t_order WHERE orderDate LIKE concat('%',#{yueDate},'%')")
    Integer findCountByorderDate(String yueDate);

    @Select("SELECT COUNT(*) FROM t_order WHERE orderStatus = '已到诊' AND orderDate LIKE concat('%',#{yueDate},'%')")
    Integer findStatusByMouth(String yueDate);

    @Select("SELECT COUNT(*) FROM t_order WHERE orderDate = #{reportDate}")
    Integer findCountByNowDate(String reportDate);

    @Select("SELECT COUNT(*) FROM t_order WHERE orderDate BETWEEN #{startDay} AND #{endDay}")
    Integer findOrderDateCountByWeek(HashMap<String, String> hashMap);

    @Select("SELECT s.`name`, COUNT(*) AS setmeal_count FROM t_setmeal s,t_order o WHERE s.`id`=o.`setmeal_id` GROUP BY s.`id`")
    List<MapVO> findhotSetmeal1();
    @Select("select count(*) from t_order")
    Integer findAllorder();

    @Select("SELECT  COUNT(*) FROM t_order WHERE orderDate LIKE concat('%',#{repalce},'%')")
    Integer findMemberCountByMouth(String replace);
    @Select("SELECT * FROM t_order WHERE member_id = #{id}")
    @Results({
            @Result(property = "id",column = "id"),
            @Result(property = "memberId",column = "member_id"),
            @Result(property = "orderDate",column = "orderDate"),
            @Result(property = "orderType",column = "orderType"),
            @Result(property = "orderStatus",column = "orderStatus"),
            @Result(property = "setmealId",column = "setmeal_id"),
    })
    Order findOrderByMemberId(Integer id);
    @Select("select count(id) from t_order where address_id=#{param1}")
    Integer findOrderByAddressId(String id);

}
